// pages/teacher/teachermanage/teachermanage.js
import sendRequest from '../../rpromise/request'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0,
        tabs: ['正在审核', '正在进行','发布历史'],
        index: null,
        date:'2023-01',
        activity:{},
    },
    tabSelect(e) {
        // console.log(e.currentTarget.dataset.id)
        this.setData({
            current: e.currentTarget.dataset.id
        })
    },
    bindDateChange(e){
        console.log(e.detail.value)
        this.setData({
            date:e.detail.value
        })
    },
    LookActivity(e){
        var i = e.currentTarget.dataset.id
        var adata = JSON.stringify(this.data.activity[i])
        console.log(adata)
        wx.navigateTo({
            url: '/pages/teacher/teacheractivity/teacheractivity?adata=' + adata,
        })
    },
    deleteBtn(e) {
        console.log(e.currentTarget.dataset.index)
        var that = this
        var index = e.currentTarget.dataset.index
        var activity = this.data.activity
        // if(activity[index].audit==='正在报名'||activity[index].audit==='正在进行'){
        //     wx.showToast({
        //         title: '不可删除，当前活动正在报名或正在进行中！',
        //         icon: 'none'
        //     })
        //     return
        // }
        // if
        wx.showModal({
            title: '提示',
            content: '确定要删除此活动吗？',
            complete: (res) => {
                if (res.cancel) {

                }
                if (res.confirm) {
                    var sql = "DELETE FROM activity where ano="+activity[index].ano
                    that.sqlRequest(sql)
                    activity.splice(index, 1);
                    that.setData({
                        activity:activity
                    })
                    wx.showToast({
                      title: '已删除！',
                      icon:'success'
                    })
                }
            }
        })
    },
    editBtn(e) {
        var i = e.currentTarget.dataset.id
        var adata = JSON.stringify(this.data.activity[i])
        console.log(adata)

        wx.navigateTo({
            url: '/pages/teacher/editactivity/editactivity?adata=' + adata,
        })
    },
    async LoadActivity(){
        var sql = 'select * from activity where tno=' + wx.getStorageSync('teacher').no
        console.log(wx.getStorageSync('teacher'))
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql:sql,
            },
        })
        console.log(res1.data)
        this.setData({
            activity:res1.data
        })
        // this.showActivity()
    },
    async sqlRequest(sql){
        const res1 = await sendRequest({
            method: 'POST',
            url: '/manage',
            data: {
                sql: sql,
            },
        })
        return res1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        wx.setNavigationBarTitle({
            title: '活动管理',
        })
        this.LoadActivity()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})